﻿package{
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Loader;
	import flash.text.TextField;
	import flash.geom.Rectangle;
	import flash.geom.Matrix;
	import flash.utils.Timer;
	import flash.utils.ByteArray;
	import flash.ui.Mouse;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequestMethod;
	import flash.net.FileReference;
	import flash.net.FileFilter;
	import flash.events.*;
	
	public class PicView extends Sprite {
		
		//图片容器
		private var picSprite:Sprite;
		
		//矩形选择框
		private var rectView:RectView;
		private var _w:int;
		private var _h:int;
		
		//图片Bitmap
		private var bm:Bitmap;
		
		//截图Bitmap
		private var smallImg:Bitmap;
		
		//图片BtimapData
		private var bmData:BitmapData;
		private var newBM:Bitmap;
		
		//截图BitmapData
		private var newImg:BitmapData;
		
		//截图容器
		private var newContent:Sprite;
		
		//上传文件的http地址、文件类、文件类型
		private var uploadURL:URLRequest;
		private var file:FileReference;
		private var fileType:String
		
		//上传保存地址、路径
		private var upURL:String;
		private var saveURL:String;
		private var upPath:String;
		private var savePath:String;
		
		//构造方法
		public function PicView(){
			
			_w = root.loaderInfo.parameters.w;
			_h = root.loaderInfo.parameters.h;
			
			//out_txt.text = root.loaderInfo.parameters.w ;
			
			rectView = new RectView(_w, _h);
			addChild(rectView);
			
			save_btn.addEventListener(MouseEvent.MOUSE_DOWN, onSaveDownHandler);
			
			newContent = new Sprite();
			addChild(newContent);
			
			uploadURL = new URLRequest();


			file = new FileReference();
			file.addEventListener(Event.SELECT, selectHandler);
			file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			file.addEventListener(Event.COMPLETE, completeHandler);
			
			browseBtn.addEventListener(MouseEvent.MOUSE_DOWN,browse);
			
			//var param:Object = root.loaderInfo.parameters;
			upURL = root.loaderInfo.parameters.upURL;//"http://localhost/GamertagV1.0/upFile.aspx"
			saveURL = root.loaderInfo.parameters.saveURL;//"http://localhost/GamertagV1.0/UpLoad.aspx";
			upPath = root.loaderInfo.parameters.upPath;//"upLoaded/";
			//out_txt.text = root.loaderInfo.parameters.upURL + root.loaderInfo.parameters.saveURL + root.loaderInfo.parameters.upPath;
			
		}
		
		//初始化
		public function init(url:String):void{
			try{
				var loader:Loader = new Loader;
				loader.load(new URLRequest(url));
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
				rectView.addEventListener(MouseEvent.MOUSE_MOVE, onMoveHandler);
				stage.addEventListener(MouseEvent.MOUSE_UP, onUpHandler);
				
			}catch (error:ArgumentError) {
				out_txt.text = String(error);
				trace(error);
			} catch (error:SecurityError) {
				out_txt.text = String(error);
				trace(error);
			}
			

		 }
		
		//加载事件
		private function onCompleteHandler(event:Event):void{

			picSprite = weakEffectPic(event.currentTarget.loader);
			addChild(picSprite);
			swapChildren(rectView,browseBtn);
			swapChildren(picSprite, rectView);
			swapChildren(picSprite,save_btn);
			
		}
		
		
		//淡化效果
		private function weakEffectPic(loader:Loader):Sprite{
			bm = Bitmap(loader.content);
			while(bm.width > 377 || bm.height > 380){
				bm.scaleX -= 0.001;
				bm.scaleY = bm.scaleX;
			}
			
			var w:Number = bm.width;
			var h:Number = bm.height;
			bm.x = (377 - w)/2;
			bm.y = (380 - h)/2;
			
			rectView.setX = bm.x;
			rectView.setY = bm.y;
			rectView.setW = w - rectView.r_w;
			rectView.setH = h - rectView.r_h;

			bmData = new BitmapData(377, 380, true, 0x99ffffff);
			newBM = new Bitmap(bmData);
			
			//bmData.fillRect(new Rectangle(rectView.x + rectView.r_x,rectView.y + rectView.r_y, rectView.r_w, rectView.r_h),0x0000000);
			
			//trace(rectView.width);
			
			var sp:Sprite = new Sprite();
			sp.addChild(bm);
			sp.addChild(newBM);
			
			//trace(newBM.x);
			trace(this.numChildren);
			return sp;
			
		}
		

		
		private function onMoveHandler(event:MouseEvent):void{
			
			event.updateAfterEvent();
			
			
			bmData.fillRect(new Rectangle(0,0, 550, 400),0x99ffffff);
			
			bmData.fillRect(new Rectangle(rectView.x + rectView.r_x,rectView.y + rectView.r_y, rectView.r_w, rectView.r_h),0x0000000);
			
		}
		
		
		//把截图存储到新的bitmap里
		private function onUpHandler(event:MouseEvent):void{
			bmData.fillRect(new Rectangle(rectView.x + rectView.r_x,rectView.y + rectView.r_y, rectView.r_w, rectView.r_h),0x0000000);
			
			var matrix:Matrix = new Matrix(bm.scaleX, 0, 0, bm.scaleY, -Math.floor(rectView.r_x - bm.x), -Math.floor(rectView.r_y - bm.y));
			
			rectView.setW = bm.width - rectView.r_w;
			rectView.setH = bm.height - rectView.r_h;
			
			newImg = new BitmapData(rectView.r_w, rectView.r_h, true, 0x00);
			newImg.draw(bm, matrix);
			
			smallImg = new Bitmap(newImg);
			smallImg.x = 0;
			smallImg.y = 0;
			
			
			
			var spt:Sprite = new Sprite();
			spt.addChild(smallImg);
			spt.x = 380;
			spt.y = bm.y;
			while(spt.width > 80 && spt.height > 100){
				spt.scaleX -= 0.01;
				spt.scaleY = spt.scaleX;
			}
			
			newContent.addChild(spt);
			
			while(newContent.numChildren > 1){
				newContent.removeChildAt(0);
			}
			
			/*trace(matrix.toString());
			trace(rectView.x + rectView.r_x);
			trace(rectView.y + rectView.r_y);*/
			/*
			trace(newContent.numChildren);
			trace("spt " + newContent.getChildIndex(spt));*/
			//trace(smallImg.bitmapData);
		}
		
		
		private function onSaveDownHandler(event:MouseEvent):void{
			savePic(smallImg);
		}
		
		//保存图片到服务
		private function savePic(bmp:Bitmap):void{

			var bpd:BitmapData = new BitmapData(bmp.width, bmp.height, true, 0x00000000);
			bpd.draw(bmp);
			var encoder:JPGEncoder = new JPGEncoder(80);
			var dat:ByteArray = encoder.encode(bpd);


			var request:URLRequest = new URLRequest(saveURL);
			request.contentType = "application/octet-stream";
			request.data = dat;
			request.method = URLRequestMethod.POST;
			var loader:URLLoader = new URLLoader();
			loader.dataFormat=URLLoaderDataFormat.BINARY;
			try {
					loader.load(request);
			} catch (error:ArgumentError) {
				trace(error);
			} catch (error:SecurityError) {
				trace(error);
			}
		}
		
		function selectHandler(event:Event):void {
			var file:FileReference = FileReference(event.target);
			trace("selectHandler: name=" + file.name + " URL=" + uploadURL.url);
			uploadTxt.text = file.name;
			fileType = file.type;
			//uploadURL.url ="http://localhost/Gamertag(8.10)/upFile_I.aspx"//?uploadName="+ file.name;
			uploadURL =new URLRequest(upURL);
			file.upload(uploadURL)
		}
		function ioErrorHandler(event:IOErrorEvent):void {
			trace("ioErrorHandler: " + event);
			uploadTxt.text ="上传失败..."
		}
		function progressHandler(event:ProgressEvent):void {
			var file:FileReference = FileReference(event.target);
			trace("progressHandler: name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
			uploadTxt.text ="正在上传..."
		}
		function completeHandler(event:Event):void {
			trace("completeHandler: " + event);
			uploadTxt.text ="上传完毕..."
			init(upPath + file.name);
			
		}

		function browse(e:Event){
			var imagesFilter:FileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");
			//var docFilter:FileFilter = new FileFilter("Documents", "*.pdf;*.doc;*.txt");
			file.browse([imagesFilter]); 
	
		}

	}
}